package cn.bdqn.smbms.controller;

import cn.bdqn.smbms.entity.Bill;
import cn.bdqn.smbms.entity.Provider;
import cn.bdqn.smbms.entity.User;
import cn.bdqn.smbms.service.BillService;
import cn.bdqn.smbms.service.ProviderService;
import cn.bdqn.smbms.util.PageBean;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/bill")
public class BillController {

    @Autowired
    private BillService billService;

    @Autowired
    private ProviderService providerService;

    //分页查询
    @RequestMapping("/bill.html")
    public String showBill(
            @RequestParam(value = "queryProductName",required = false)
                     String queryProductName,
            @RequestParam(value = "queryProviderId",required = false)
                    Integer queryProviderId,
            @RequestParam(value = "isPayment",required = false)
                    Integer isPayment,
            @RequestParam(value = "pageNo",required = false,defaultValue = "1")
                    Integer pageNo,
            Model model
    ){
        int pageSize=3;
        PageBean<Bill> pageBean = billService.findByPage(queryProductName,queryProviderId,isPayment,pageNo,pageSize);
        List<Provider> provider = providerService.findAll();
        model.addAttribute("pageBean",pageBean);
        model.addAttribute("providerList",provider);
        if (queryProductName != null) {
            model.addAttribute("queryProductName", queryProductName);
        }
        if (queryProviderId != null) {
            model.addAttribute("queryProviderId", queryProviderId);
        }
        if (isPayment != null) {
            model.addAttribute("isPayment", isPayment);
        }
        return "billlist";
    }

    //显示添加订单
    @RequestMapping("/billadd.html")
    public String showBillAdd(){
        return "billadd";
    }

    //添加订单
    @RequestMapping("/addBill")
    public String addBill(Bill bill,HttpSession session){
        User userSession = (User)session.getAttribute("userSession");
        bill.setCreatedBy(userSession.getId());
        int ret = billService.addBill(bill);
        if(ret>0){
            return "success1";
        }else{
            return "error1";
        }
    }

    //显示供应商
    @ResponseBody
    @RequestMapping(value = "/bill.do",produces = {"application/json;charset=UTF-8"})
    public List<Provider> showPro(){
        List<Provider> providers = providerService.findAll();
        return providers;
    }

    //根据Id查询
    @RequestMapping("/modify/{id}")
    public String showId(
            @PathVariable
            Integer id,
            Model model
    ){
        Bill bill = billService.findById(id);
        model.addAttribute("bill",bill);
        return "billmodify";
    }

    //修改订单
    @RequestMapping("/billModify")
    public String billModify(Bill bill,HttpSession session){
        User userSession = (User)session.getAttribute("userSession");
        bill.setModifyBy(userSession.getId());
        int ret = billService.updateBill(bill);
        if(ret>0){
            return "success1";
        }else{
            return "error1";
        }
    }

    //删除订单
    @ResponseBody
    @RequestMapping("/delBill/{id}")
    public String delBill(
            @PathVariable("id")
            Integer id
    ){
        Map<String,Object> map = new HashMap<>();
        //查一下有无此订单
        Bill bill = billService.findById(id);
        if(bill==null){
            map.put("delResult","notexist");
        }else{
            int ret = billService.delBill(id);
            if(ret>0){
                map.put("delResult","true");
            }else {
                map.put("delResult","false");
            }
        }
        return JSON.toJSONString(map);
    }

    //ajax查看订单详情
    @ResponseBody
    @RequestMapping(value = "/showBill/{id}", produces = {"application/json;charset=UTF-8"})
    public Bill showBill(
            @PathVariable("id")
            Integer id
    ){
        Bill bill = billService.findById(id);
        return bill;
    }

    //利用ajax验证订单编码是否存在
    @ResponseBody
    @RequestMapping("/checkBillCode")
    public boolean checkBillCode(
            @RequestParam
            String billCode
    ){
        return billService.checkBillCode(billCode);
    }

}